package com.yxy.service;

import com.github.pagehelper.PageHelper;
import com.yxy.base.BusinessException;

import com.yxy.dao.PersonMapper;

import com.yxy.dao.TableDepartmentMapper;
import com.yxy.dto.TableDepartmentDto;
import com.yxy.feign.VO.AdminUserInToken;

import com.yxy.model.TableDepartment;
import com.yxy.utils.TenantHelper;
import com.yxy.utils.TokenCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@Service
public class TableDepartmentService {


  @Resource
  private HttpServletRequest request;

  @Autowired
  private TokenCheckUtil tokenCheckUtil;

  @Resource
  private PersonMapper personMapper;
  @Resource
  private TableDepartmentMapper tableDepartmentMapper;







  @Autowired
  private TenantHelper tenantHelper;


  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public TableDepartment save(TableDepartmentDto pojo) throws BusinessException {
    AdminUserInToken adminUser = tokenCheckUtil.getUserInfoByToken();

    TableDepartment entity = new TableDepartment();
    BeanUtils.copyProperties(pojo, entity);
    entity.setCreateDate(new Date());
    entity.setCreateUserId(adminUser.getAdminUser().getId());
    entity.setCreateUserName(adminUser.getAdminUser().getUsername());
    tableDepartmentMapper.insertSelective(entity);
    return entity;
  }








  //编辑数据
  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public TableDepartment modify(TableDepartmentDto pojo) throws BusinessException {
    TableDepartment gpfMarketProductcode = tableDepartmentMapper.selectByPrimaryKey(pojo.getId());
    if (gpfMarketProductcode == null) {
      throw new BusinessException(501, "修改数据失败，未找到Id为[" + pojo.getId() + "]的数据");
    }

    AdminUserInToken adminUser = tokenCheckUtil.getUserInfoByToken();
    TableDepartment entity = new TableDepartment();
    BeanUtils.copyProperties(pojo, entity);
    entity.setUpdateDate(new Date());
    try {
      entity.setUpdateUserId(adminUser.getAdminUser().getId());
    } catch (NullPointerException ne) {
      entity.setUpdateUserId(Long.parseLong("3"));
    }
    try{
      entity.setUpdateUserName(adminUser.getAdminUser().getUsername());
    }catch (NullPointerException ne){
      entity.setUpdateUserName("admin");
    }
    tableDepartmentMapper.updateByPrimaryKeySelective(entity);
    return entity;
  }






  //获取加工企业登记审核列表
  public List<TableDepartment> getlist(TableDepartmentDto query) throws BusinessException {
    AdminUserInToken adminUser = tokenCheckUtil.getUserInfoByToken();
    // 处理tenant id list
    Set<Long> tenantIdList = new HashSet<>();
    Long tenantId = query.getTenantId();
    if (tenantId == null) {// 取出用户所属租户下所有的数据
      tenantIdList = adminUser.getTenantIdList();
    } else {
      if (!adminUser.getTenantIdList().contains(tenantId)) { // 判断前端传递的tenantId是否属于该用户
        throw new BusinessException(510, "前端传递的tenantId不属于该用户");
      }
      tenantIdList.add(tenantId);
    }

    query.setTenantIdList(tenantIdList);
    PageHelper.startPage(query.getPageNumber(), query.getPageSize());
    PageHelper.orderBy("t1.create_date desc");//排序
    List<TableDepartment> dbResult = tableDepartmentMapper.selectList(query);
    return dbResult;
  }






  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void delete(String Id, Integer status) throws BusinessException {
    String arr[] = Id.split(",");
    List<Long> idlist = new ArrayList<>();
    for (int i = 0; i < arr.length; i++) {
      Long p = Long.parseLong(arr[i]);
      idlist.add(p);
    }
    if (CollectionUtils.isEmpty(idlist)) {
      throw new BusinessException(501, "删除失败，id为null");
    }
    int i = tableDepartmentMapper.deleteByIds(idlist);
    if (i <= 0) {
      throw new BusinessException(501, "删除数据失败");
    }
  }


}
